package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Customer;

public class UnRegCustomer extends JPanel{

	private static final long serialVersionUID = 119246125116363994L;

	private Customer cust;
	private JPanel custPanel = null;
	private JPanel lPanel = null, loPanel = null;
	private JTextField cidText, pText;
	private JButton loginButton, logoutButton, unSub;
	private String cID = null, pWrd = null;
	
	public UnRegCustomer(Customer cust) {
		this.cust = cust;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		loginPanel();
	}
	
	// panel for logging in
	private void loginPanel() {
		lPanel = new JPanel(new FlowLayout());
		lPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		lPanel.add(new JLabel("Customer ID: "));
        cidText = new JTextField();
        cidText.setPreferredSize(new Dimension(220, 30));
        lPanel.add(cidText);

        lPanel.add(new JLabel("Password: "));
        pText = new JTextField();
        pText.setPreferredSize(new Dimension(220, 30));
        lPanel.add(pText);
        
		loginButton = new JButton("Login");
		loginButton.addActionListener(new verifyLogin());
		lPanel.add(loginButton);
		
		add(lPanel);
	}
	
	// get the stats for a particular customer
	private void customerStatsPanel() {
		custPanel = new JPanel(new BorderLayout());
		JTextArea textArea = new JTextArea();
		JScrollPane stats = new JScrollPane(textArea);
		String nam = null, adrss = null, ph = null;
		
		// get the customer stats
		ResultSet cStats = null;
		try {
			cStats = cust.retrieveUserStats(cID, pWrd);
		} catch (SQLException e) {
			System.out.println("Customer could not be found.");
		}
		
		// extract customer data from the result set
		try {
			if(cStats.next()) {
				nam = cStats.getString(3);
				adrss = cStats.getString(4);
				ph = cStats.getString(5);
			}
			
			cStats.close();
		} catch (SQLException e) {
			System.out.println("Customer stats could not be extracted.");
		}
		
		// make a titled border
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Customer Account");
		custPanel.setBorder(title);
		
        // make text area non-editable and line wrap
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        // set orientation and of text area
        custPanel.add(stats, BorderLayout.CENTER);
		
        add(custPanel);
		
		textArea.append("Customer Account Summary:" + "\n" +
				"\n" + "Customer ID: " + cID + 
				"\n" + "Password: " + pWrd + 
				"\n" + "Name: " + nam + 
				"\n" + "Address: " + adrss + 
				"\n" + "Phone: " + ph +
				"\n\n" + "Please click the 'Unsubscribe' button to close this account" +
				"\n" + "or click the 'Logout' button to cancel and return to the login screen.");
	}
	
	// buttons used in the panel
	private void buttonPanel() {
			loPanel = new JPanel(new GridLayout(1,2,10,5));
			
			// unsubscribe button
			unSub = new JButton("Unsubscribe");
			unSub.addActionListener(new cancelAccount());
			loPanel.add(unSub);
			
			// logout button
			logoutButton = new JButton("Logout");
			logoutButton.addActionListener(new verifyLogout());
			loPanel.add(logoutButton);
			
			add(loPanel);
	}	
	
	// action listener for the login button
	private class verifyLogin implements ActionListener {
		
		public void actionPerformed(ActionEvent e0) {
			cID = cidText.getText();
			pWrd = pText.getText();
			
			if(cID.isEmpty() || pWrd.isEmpty()) {
				JOptionPane.showMessageDialog(loginButton,
					    "Please check that the following have been entered:\n- Customer ID\n- Password",
					    "Login Error",
					    JOptionPane.ERROR_MESSAGE);
			} else {
				boolean success = false;
				
				try {
					success = cust.findCustomer(cID, pWrd);
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(loginButton,
						    "Customer ID and Password do not match.\nPlease re-enter your Customer ID and Password or Please Register.",
						    "Login Error",
						    JOptionPane.ERROR_MESSAGE);
				}
				
				// if login is successful, create the purchasing panels
				if(success == true) {
					lPanel.setVisible(false);
					
					customerStatsPanel();
					buttonPanel();

				} else {
					JOptionPane.showMessageDialog(loginButton,
						    "Customer ID and Password do not match.\nPlease re-enter your Customer ID and Password or Please Register.",
						    "Login Error",
						    JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	// action listener for the logout button
	private class verifyLogout implements ActionListener {
		
		public void actionPerformed(ActionEvent e1) {
			
			//set items and basket panels invisible
			custPanel.setVisible(false);
			loPanel.setVisible(false);
			
			// remove the items and basket panels
			removeAll();
			
			// recreate the login
			loginPanel();
		}
				
	}

	// action listener for the unsubscribe button
	private class cancelAccount implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			cust.cancelAccount(cID,pWrd);
			
			//set items and basket panels invisible
			custPanel.setVisible(false);
			loPanel.setVisible(false);
			
			// remove the items and basket panels
			removeAll();
			
			// recreate the login
			loginPanel();
		}
	}
}
